package com.jame.design_mode.proxy;

import com.jame.design_mode.proxy.cglib_proxy.CglibProxy;
import com.jame.design_mode.proxy.dynamic_proxy.DynamicProxy;
import com.jame.design_mode.proxy.static_proxy.StaticProxy;
import org.junit.Test;

/**
 * @author : Jame
 * @date : 2021-06-30 10:07
 **/
public class TestProxy {
    public static void main(String[] args) {
        Demo demo = new Demo();
        StaticProxy proxy = new StaticProxy();
        proxy.setDemo(demo);
        proxy.Get("test");
    }

    @Test
    public void test() {
        //使用jdk自带proxy来实现动态代理
        //接收类型为代理类的接口,强转也是为接口
        DynamicProxy dynamicProxy = new DynamicProxy();
        DemoInterface demo = (DemoInterface) dynamicProxy.newProxyInstance(new Demo("22"));
        demo.Get("ss");
    }


    @Test
    public void test1() {
        Demo demo1 = new Demo();
        demo1.setName("小王");
        //使用cglib实现动态代理
        CglibProxy proxy = new CglibProxy();
        //通过生成子类的方式创建代理类
        Object o = proxy.getProxy(demo1.getClass());
        System.out.println(o instanceof  Demo);
        Demo demo = (Demo) proxy.getProxy(Demo.class);
        System.out.println(demo.getName());
//        demo.Get("s");
//        demo.set();

    }
}
